/* eslint-disable */
import axios from 'axios';
import crypto from 'crypto-js';
import fig from '../config/config.js';
import router from '../router';
import Vue from 'vue'
/* 设置头 */
const appid = crypto.enc.Latin1.parse(fig.APPID);
const key = crypto.enc.Latin1.parse(fig.KEY);
const iv = crypto.enc.Latin1.parse(fig.IV);
const tokenn = crypto.enc.Latin1.parse(fig.TOKEN);
/* 拦截器 */
axios.interceptors.request.use((config) => {
  const url = config.url+'';
  console.log(config)
  if (url.indexOf('login') == -1 && url.indexOf('selectBulletin') == -1 && url.indexOf('updateImages') == -1) {
      console.log(config);
      const repdata = config.data.data;
      console.log(repdata);
      const encRes = crypto.AES.encrypt(repdata, crypto.enc.Latin1.parse(fig.KEY), { iv, padding: crypto.pad.ZeroPadding });
      const lol = crypto.MD5(repdata);
      const token = fig.TOKEN;
      const timestamp = config.data.timestamp;
      const arry = [lol,token, timestamp, fig.APPID];
      arry.sort();
      arry.reverse();
      const arryStr = `${arry[0]}${arry[1]}${arry[2]}${arry[3]}`;
      const sig = crypto.SHA1(arryStr).toString(crypto.enc.Hex);
      config.headers.timestamp = timestamp;
      config.headers.signature = sig;
      config.headers.lol = lol;
      config.headers.appid = fig.APPID;
      config.headers.token = token;
      config.data.data = encRes.toString();
      console.log(config.data.areaId)
      if(config.data.areaId === undefined){
        config.params = {
          data: encRes.toString(),
          timestamp,
        };
      }else{
        config.params = {
          data: encRes.toString(),
          timestamp,
          areaId:config.data.areaId,
        };
      }

  } else {
    if(config.data.areaId === undefined){
      config.params = {
        data: config.data.data,
        timestamp: config.data.timestamp,
      };
    }else{
      config.params = {
        data: config.data.data,
        timestamp: config.data.timestamp,
        areaId:config.data.areaId,
      };
    }
  }
  return config;
}, error => Promise.reject(error));

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么response.config.url
  const url = response.config.url+'';
  if (response.status === 200) {
    if (url.indexOf('login') != -1 || url.indexOf('selectBulletin') != -1 || url.indexOf('updateImages') != -1) {
      return response;
    }
    console.log(  response.data);

    if(response.data.code === 100 || response.data.code === 110 || response.data.code === 112 || response.data.code === 111 || response.data.code === 113){
      return response;
    }
    const resdata = response.data;
    const decRes = crypto.AES.decrypt(resdata, crypto.enc.Latin1.parse(fig.KEY), { iv, padding: crypto.pad.ZeroPadding });
    console.log(decRes.toString(crypto.enc.Utf8));
    response.data =  JSON.parse(decRes.toString(crypto.enc.Utf8));
  }
  return response;
}, function (error) {
  if(error && error.response){
    if (error.response.status === 401) {
      /** 跳回登录页面 */
      sessionStorage.removeItem('user');
      router.push({name:'login',params:{flag:401,msg:'登录过期请重新登录'}});
    } else if (error.response.status === 402) {
      console.log(`返回状态错误，状态码为:${error.response.status}`);
      sessionStorage.removeItem('user');
      router.push({name:'login',params:{flag:403,msg:'签名认证识别,请联系开发商'}});
    } else if (error.response.status === 403) {
      console.log(`返回状态错误，状态码为:${error.response.status}`);
      sessionStorage.removeItem('user');
      router.push({name:'login',params:{flag:403,msg:'签名认证识别,请联系开发商'}});
    } else{
      console.log(`返回状态错误，状态码为:${error.response.status}`);
    }
  }else{
    
  }
  return Promise.reject((error))
});


/**
 * 登陆请求http
 * @param {请求传递的参数} params
 */
export const onLogin = params => axios.post(fig.IP, params).then(res => res.data);


/**
 * 登陆请求http
 * @param {请求传递的参数} params
 */
export const onRequestt = params => axios.post(fig.IP, params).then(res => res.data);


/**
 * 上传图片
 * @param {请求传递的参数} params
 */
export const onUpdate = params => axios.post(fig.IP, params).then(res => res.data);